<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="confirm">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [], // 已选角色列表
      roleList: [] // 可选角色列表
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close-dialog')
    },
    async loadRoleList() {
      const res = await getRoleList({ page: 1, pageSize: 1 })
      this.roleList = res.data.rows
    },
    openDialog() {
      this.loadRoleList()
      this.loadRoleInfo()
    },
    // 分配角色
    async confirm() {
      await assignRoles({ id: this.curId, roleIds: this.checkList })
      this.$message({
        type: 'success',
        message: '分配角色成功'
      })
      this.closeDialog()
    },
    // 获取用户角色数据
    async loadRoleInfo() {
      const res = await getUserDetailById(this.curId)
      console.log(res)
      this.checkList = res.data.roleIds
    }
  }
}
</script>
